package com.svse.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * @author LiYuhang
 * @version 0.1
 * @application  加密工具类
 * @Copyright 上海危网信息科技有限公司版权所有
 * @company wwsoft
 * @Date 2020/1/14 8:35
 */
@Slf4j
public class EncryptionUtils {

    private static String passWord="admin123456";

    public static void main(String[] args) {
       /* String pwd = "admin123456";
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodePwd = encoder.encode(pwd);
        String encodePassWord = encoder.encode(passWord);
        log.info("equals:{}", StringUtils.equals(passWord,pwd));
        log.info("encode_pwd:{}", encodePwd);
        log.info("encode_passWord:{}", encodePassWord);
        log.info("mathPassword:{}", encoder.matches(pwd,encodePassWord));*/
    }

    public  static String encode_apache_Md5(String src) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] encodeBytes = md.digest(src.getBytes());
            return Hex.encodeHexString(encodeBytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static   String  encode_jdk_Md5(String src) {
        try {
            //获取MD5实例
            MessageDigest md = MessageDigest.getInstance("MD5");
            //此处传入要加密的byte类型值
            md.update(src.getBytes());
            //此处得到的是md5加密后的byte类型值
            byte[] digest = md.digest();
            int i;
            StringBuilder sb = new StringBuilder();
            for (int offset = 0; offset < digest.length; offset++) {
                i = digest[offset];
                if (i < 0){
                    i += 256;
                }
                if (i < 16){
                    sb.append(0);
                }
                //通过Integer.toHexString方法把值变为16进制
                sb.append(Integer.toHexString(i));
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public  static String encode_jdk_base64(String src) {
         src= Base64.getEncoder().encodeToString(src.getBytes());
         log.info("加密:{}",src);
         return src;
    }

    public  static String decode_jdk_base64(String src) {
        byte[] byteArr=Base64.getDecoder().decode(src);
        String txet=new String(byteArr);
        log.info("解密:{}",txet);
        return txet;
    }

    public  static String encode_apache_base64(String src) {
        byte[] byteArr=org.apache.commons.codec.binary.Base64.encodeBase64(src.getBytes());
        String msg=new String(byteArr);
        log.info("加密:{}",msg);
        return msg;
    }

    public  static String decode_apache_base64(String src) {
        byte[] byteArr2=org.apache.commons.codec.binary.Base64.decodeBase64(src);
        String msg2=new String(byteArr2);
        log.info("解密:{}",msg2);
        return msg2;
    }
}
